package example.controller;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskHandle;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.appengine.api.taskqueue.TaskQueuePb.TaskQueueQueryAndOwnTasksResponse.Task;

public class PullQueues extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		Queue q = QueueFactory.getQueue("pull-default");
		q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
                .payload("parse1")
                .tag("parse".getBytes()));
		q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
		                .payload("parse2")
		                .tag("parse".getBytes()));
		q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
		                .payload("render1")
		                .tag("render".getBytes()));
		q.add(TaskOptions.Builder.withMethod(TaskOptions.Method.PULL)
		                .payload("render2")
		                .tag("render".getBytes()));
		
		//Lease render tasks, but not parse
		List<TaskHandle> list = q.leaseTasksByTag(3600, TimeUnit.SECONDS, 100, "render");
		for (int i = 0; i < list.size(); i++) {
			response.getWriter().println(list.get(i).getName());
		}
		
//		q.deleteTask("task1");
	}
}
